package com.gofaraway.service.左神算法课;

/**
 * @author ChenPeng
 * @Date 2024/12/28 23:36
 */
public class Code05_ReverseList {


    /**
     * 单链表反转
     *
     * @param head
     * @return
     */
    public static Node reverseLinkedList(Node head) {
        Node pre = null;
        Node next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    /**
     * 双链表的反转
     *
     * @param head
     * @return
     */
    public static DoubleNode reverseDoubleLinkedList(DoubleNode head) {

        DoubleNode pre = null;
        DoubleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;
    }


    static class DoubleNode {
        public int value;

        public DoubleNode next;

        public DoubleNode last;
    }

    static class Node {

        public int value;

        public Node next;
    }
}
